﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace TestGenerator
{
    class Parser
    {
        private List<Question> pool;

        private string header;
        private string footer;

        public Parser(string filename)
        {
            pool = new List<Question>();
            string html = File.ReadAllText(filename, Encoding.Default);

            header = html.Substring(0, html.IndexOf("<p class=Question>"));

            int lastMatchIndex = 0;

            Match match = Regex.Match(html, @"<p class=Question>(.|\s)*?</p>");

            int i = 0;
            while (match.Success)
            {
                Match nextMatch = match.NextMatch();
                lastMatchIndex = Math.Max(lastMatchIndex, match.Index);

                int from = match.Index;
                int to = (nextMatch.Success) ? nextMatch.Index : html.Length - 1;
                pool.Add(new Question(match.ToString().Substring(
                    match.ToString().IndexOf('>') + 1,
                    match.ToString().LastIndexOf('<') - "<p class=Question>".Length )
                    ));

                Match corrects = Regex.Match(html.Substring(from, to - from + 1), @"<p class=CorrectAnswer>(.|\s)*?</p>");

                while (corrects.Success)
                {
                    lastMatchIndex = Math.Max(lastMatchIndex, corrects.Index);

                    int startIndex = corrects.ToString().IndexOf('>') + 1 + "Correct. ".Length;
                    int length = corrects.ToString().LastIndexOf('<') - "<p class=CorrectAnswer>Correct. ".Length;
                    if (startIndex >= corrects.Length || length <= 0)
                    {
                        throw new Exception("Incorrect Correct Answer format!");
                    }
                    pool[pool.Count - 1].AddCorrectAnswer(corrects.ToString().Substring(
                    startIndex, length));
                    corrects = corrects.NextMatch();
                }

                Match wrongs = Regex.Match(html.Substring(from, to - from + 1), @"<p class=WrongAnswer>(.|\s)*?</p>");
                while (wrongs.Success)
                {
                    lastMatchIndex = Math.Max(lastMatchIndex, wrongs.Index);
                    int startIndex = wrongs.ToString().IndexOf('>') + 1 + "Wrong. ".Length;

                    int length = wrongs.ToString().LastIndexOf('<') - "<p class=WrongAnswer>Wrong. ".Length;
                    if (startIndex >= wrongs.Length || length <= 0)
                    {
                        throw new Exception("Incorrect Wrong Answer format!");
                    }
                    pool[pool.Count - 1].AddWrongAnswer(wrongs.ToString().Substring(startIndex, length));
                    wrongs = wrongs.NextMatch();
                }

                footer = html.Substring(Math.Max(
                    Math.Max(html.LastIndexOf("<p class=Question>"), html.LastIndexOf("<p class=WrongAnswer>")),
                    html.LastIndexOf("<p class=CorrectAnswer>"))
                    );
                footer = footer.Substring(footer.IndexOf("</p>") + "</p>".Length);
                match = nextMatch;
            }
        }

        public List<Question> Questions
        {
            get
            {
                return pool;
            }
        }

        public string Header
        {
            get
            {
                return header;
            }
        }

        public string Footer
        {
            get
            {
                return footer;
            }
        }
    }
}
